<?php

namespace app\api\controller\kuyou\applet;
use app\api\library\kuyou\Auths;
use think\Cache;
use think\Db;

class User extends Common
{
    protected $noNeedLogin = ['login','getlist'];
    protected $noNeedRight = '*';
    protected $auth_s;

    public function _initialize()
    {
        parent::_initialize();
        $this->auth_s = new Auths();
    }

    /**
     * 会员登录
     *
     * @ApiMethod (POST)
     * @param string $account  账号
     * @param string $password 密码
     */
    public function login()
    {
        $account = $this->request->post('account');
        $password = $this->request->post('password');
        if (!$account || !$password) {
            $this->error(__('请输入账户和密码'));
        }
        $ret = $this->auth_s->login($account, $password);
        if ($ret) {
            $data = ['userinfo' => $this->auth_s->getUserInfo('',$ret)];
            $this->success(__('登陆成功'), $data);
        } else {
            $this->error($this->auth_s->getError());
        }
    }


    /**
     * 退出登录
     * @ApiMethod (POST)
     */
    public function logout()
    {
        $token = $this->request->server('HTTP_TOKEN', $this->request->request('token', \think\Cookie::get('token')));
        if (!$this->request->isPost()) {
            $this->error(__('参数错误'));
        }
        $this->auth_s->logout($token);
        $this->success(__('退出成功'));
    }

    /**
     * 通过token获取用户信息
     * @ApiMethod (POST)
     */
    public function userInfo($json = false)
    {
        $token = $this->request->server('HTTP_TOKEN', $this->request->request('token', \think\Cookie::get('token')));
        $data = $this->auth_s->getUserInfo('',$token);
        if(isset($data['salt'])){
            unset($data['salt']);
        }
        if(isset($data['password'])){
            unset($data['password']);
        }
        if(isset($data['updatetime'])){
            unset($data['updatetime']);
        }
        if(isset($data['logintime'])){
            $data['logintime'] = date('Y-m-d H:i:s',$data['logintime']);
        }
        if(isset($data['createtime'])){
            $data['createtime'] = date('Y-m-d H:i:s',$data['createtime']);
        }
        $data['avatar'] = $data['avatar'] ? cdnurl($data['avatar'], true) : '';
        if ($json == true) {
            return $data;
        }
        $this->success('用户信息', ['userinfo'=>$data],'200');
    }

    /**
     * 获取制单人/审核人
     * @ApiMethod (POST)
     * @param int $page 页码
     * @param int $limit 每页条数
     * @param string $name 商品名称
     */
    public function getlist()
    {
        if ($this->request->isPost()) {
            $page = $this->request->post('page');
            $page = empty($page)?1:$page;
            $limit = $this->request->post('limit');
            $limit = empty($limit)?config('app_limit'):$limit;

            $where = [];
            $keyword = $this->request->post('keyword');
            if(!empty($keyword)){
                $where['username|mobile|email'] = ['like','%'.$keyword.'%'];
            }else{
                $code = $this->request->post('username');
                if(!empty($code)){
                    $where['username'] = ['like','%'.$code.'%'];
                }
                $mobile = $this->request->post('mobile');
                if(!empty($mobile)){
                    $where['mobile'] = ['like','%'.$mobile.'%'];
                }
                $email = $this->request->post('email');
                if(!empty($email)){
                    $where['email'] = ['like','%'.$email.'%'];
                }
            }

            $result = Db::name('admin')->where($where)->field("id,username,nickname,avatar,status")->page($page,$limit)->select();

            if($result){
                $this->success('查询成功',$result,200);
            }else{
                $this->error('查询失败','',406);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }
}
